package com.hotel.controllers;
import com.hotel.entitys.*;
import com.hotel.services.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
@RestController
@Api(tags="入住记录管理接口")//tags:说明信息
public class CheckinController {
    @Autowired
    private CheckinService checkinService;
    @Autowired
    private GuestService guestService;
    @Autowired
    private StaffService staffService;
    @Autowired
    private RoomService roomService;
    @Autowired
    RevenueService revenueService;
    @ApiOperation(
            value = "客户登记插入数据",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )

    @PostMapping("/checkin/save")
    public RestPOJO add(@RequestBody Map<String,Object> map) {
        RestPOJO restPOJO = new RestPOJO();
        Checkin checkin = new Checkin();
        String guest_telephone1 = (String) map.get("guest_telephone1");
        if (guest_telephone1 != "") {
            Guest guest1 = guestService.selGuestByTel(guest_telephone1);
            checkin.setGuest_id_1(guest1.getGuest_id());
        }
        String guest_telephone2 = (String) map.get("guest_telephone2");
        if (guest_telephone2 != "") {
            Guest guest2 = guestService.selGuestByTel(guest_telephone2);
            checkin.setGuest_id_2(guest2.getGuest_id());
        }
        String guest_telephone3 = (String) map.get("guest_telephone3");
        if (guest_telephone3 != "") {
            Guest guest3 = guestService.selGuestByTel(guest_telephone3);
            checkin.setGuest_id_3(guest3.getGuest_id());
        }
        String staff_telephone = (String) map.get("staff_telephone");
        checkin.setStaff_id_in(staffService.selStaffByTel(staff_telephone));
        String room_id = (String) map.get("room_id");
        checkin.setRoom_id(Integer.parseInt(room_id));
        checkin.setRecord_room_code((String) map.get("room_code"));
        try {
            checkin.setRecord_in_date(Timestamp.valueOf((String)map.get("record_in_date")));
            checkin.setRecord_out_date(Timestamp.valueOf((String)map.get("record_out_date")));
        }catch (Exception e){
            e.printStackTrace();
        }
        checkin.setExpense((Double) map.get("moneyshow"));
        int a = checkinService.addCheckin(checkin);
        if (a > 0) {
            roomService.updRoomStatus(Integer.parseInt(room_id));
            restPOJO.setMessage("增加成功");
            restPOJO.setStatus(1000);
        } else {
            restPOJO.setMessage("增加失败");
        }
        return restPOJO;
    }


    @ApiOperation(
            value = "根据room_id查询一条入住记录",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
        @GetMapping("/check")
        public RestPOJO getCheckin (Integer room_id){
            RestPOJO restPOJO = new RestPOJO();
            Checkin checkin = checkinService.getChechin(room_id);
            if (checkin != null) {
                restPOJO.setMessage("查询一条成功");
                restPOJO.setStatus(1000);
                restPOJO.setData(checkin);
            } else {
                restPOJO.setMessage("查询一条失败");
                restPOJO.setStatus(1001);
            }
            return restPOJO;
        }

    @ApiOperation(
            value = "续费并且修改记录表",
            httpMethod = "PUT",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
        @PutMapping("/check")
        public RestPOJO getUpdate (@RequestBody Checkin checkin ){

            RestPOJO restPOJO = new RestPOJO();
            int count = checkinService.getUpdate(checkin);
            if (count > 0) {
                restPOJO.setMessage("成功续费");
                restPOJO.setStatus(1000);
                restPOJO.setData(checkin);
            } else {
                restPOJO.setMessage("修改一条失败");
                restPOJO.setStatus(1001);
            }

            return restPOJO;
        }

    @ApiOperation(
            value = "根据room_id查询一条入住记录",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
        @GetMapping("/checkcc")
        public RestPOJO getCheck (Integer room_id){
            RestPOJO restPOJO = new RestPOJO();
            Checkin checkin = checkinService.getChechin(room_id);
            if (checkin != null) {
                restPOJO.setMessage("查询一条成功");
                restPOJO.setStatus(1000);
                restPOJO.setData(checkin);
            } else {
                restPOJO.setMessage("查询一条失败");

                restPOJO.setStatus(1001);
            }
            return restPOJO;
        }


    @ApiOperation(
            value = "退房修改客户记录表",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
//        @PostMapping("/checkzz")
//        public RestPOJO getUpdat (@RequestBody Checkin checkin){
//
//            RestPOJO restPOJO = new RestPOJO();
//
//            int count = checkinService.getUpdate(checkin);
//            if (count > 0) {
//                restPOJO.setMessage("成功退房");
//                restPOJO.setStatus(1000);
//                restPOJO.setData(checkin);
//            } else {
//                restPOJO.setMessage("修改一条失败");
//                restPOJO.setStatus(1001);
//            }
//
//            return restPOJO;
//        }
    @PostMapping("/checkzzz")
    public RestPOJO getUpdatt (@RequestBody Map<String ,Object> map){
        Integer room_id = (Integer) map.get("room_id");
        RestPOJO restPOJO = new RestPOJO();
        Checkin checkin1 = checkinService.getChechin(room_id);
        int a = 0 ;
        if(checkin1.getGuest_id_1() != null){
            guestService.updById(checkin1.getGuest_id_1());
            a++;
        }
        if(checkin1.getGuest_id_2() != null){
            guestService.updById(checkin1.getGuest_id_2());
            a++;
        }
        if(checkin1.getGuest_id_3() != null){
            guestService.updById(checkin1.getGuest_id_3());
            a++;
        }
        if(a > 0) {
            Checkin checkin = new Checkin();
            String staff_telephone = (String) map.get("staff_telephone");
            checkin.setStaff_id_out(staffService.selStaffByTel(staff_telephone));
            try {
                checkin.setRecord_in_date(Timestamp.valueOf((String) map.get("record_in_date")));
                checkin.setRecord_out_date(Timestamp.valueOf((String) map.get("record_out_date")));
            } catch (Exception e) {
                e.printStackTrace();
            }
            checkin.setRoom_id(room_id);
            checkin.setRecord_room_code((String) map.get("record_room_code"));
            checkin.setExpense((Double) map.get("expense"));
            checkin.setStatus("1");
            //System.out.println(checkin+"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz");
            int count = checkinService.getUpdate(checkin);
            if (count > 0) {

                restPOJO.setMessage("成功退房");
                restPOJO.setStatus(1000);
                restPOJO.setData(checkin);
            } else {
                restPOJO.setMessage("退房失败");
                restPOJO.setStatus(1001);
            }
        }else {
            restPOJO.setMessage("退房失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }
    @ApiOperation(
            value = "根据时间改变价格",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @PostMapping("/checkin/money")
    public RestPOJO money(@RequestBody Map<String,Object> map){
        RestPOJO restPOJO = new RestPOJO();
        try {
            String room_id=(String) map.get("room_id");
            Room room = roomService.selRoomById(Integer.parseInt(room_id));
            Double room_price = room.getRoom_price();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(sdf.parse((String)map.get("record_in_date")));
            long timeInMillis1 = calendar.getTimeInMillis();
            calendar.setTime(sdf.parse((String)map.get("record_out_date")));
            long timeInMillis2 = calendar.getTimeInMillis();
            long betweenDays = ((timeInMillis2 - timeInMillis1) / (1000L*3600L*24L));
            Double money = room_price * betweenDays;
            if(money>0){
                restPOJO.setMessage("查询成功");
                restPOJO.setStatus(1000);
                restPOJO.setData(money);
            }else {
                restPOJO.setMessage("查询失败");
                restPOJO.setStatus(1001);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return restPOJO;
    }
    @ApiOperation(
            value = "查询该房间的入住信息，根据房间ID",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @GetMapping("/getCheckin")
    public RestPOJO getAll(Integer room_id) {
        RestPOJO restPOJO = new RestPOJO();
        Checkin l = checkinService.selAllCheckin(room_id);
        System.out.println(l);
        if (l != null) {
            restPOJO.setMessage("查询成功");
            restPOJO.setStatus(1000);
            restPOJO.setData(l);
        } else {
            restPOJO.setMessage("查询失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }
    @PostMapping("/selSum")
    public RestPOJO selSum(@RequestBody Checkin checkin){
        Double i = checkinService.selSum(checkin);
        revenueService.addRevenue(i,checkin.getRecord_in_date());
        RestPOJO restPOJO = new RestPOJO();
        if (i!=null){
            restPOJO.setMessage("查询成功");
            restPOJO.setStatus(1000);
            restPOJO.setData(i);
        }else {
            restPOJO.setMessage("查询失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }

}